﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PraxisRent.Entities;
using PraxisRent.BL;
using Microsoft.Practices.Unity;

namespace PraxisRent.UI
{
    public partial class FrmLogin : Form
    {
        [Dependency]
        public IUsuarioBL usuarioBL { get; set; }

        [Dependency]
        public IPersonaBL perBL { get; set; }

        public FrmLogin()
        {
            InitializeComponent();
            this.BuilUp();
            this.FormClosed += FrmMenu_FormClosed;
        }

        private void FrmMenu_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            uSUARIOBindingSource.AddNew();
        }

        /*función de encriptacion MD5 para password*/
        public string md5()
        {
            System.Security.Cryptography.MD5 md5;
            md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] encodedBytes = md5.ComputeHash(ASCIIEncoding.Default.GetBytes(passwordTextBox.Text));

            return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(encodedBytes).ToUpper(), @"-", "");
        }

        public void entrar()
        {
            var usuario = uSUARIOBindingSource.Current as USUARIO;
            var personas = perBL.GetAllFromPersonaByCriterio(txtUsuario.Text);

            if (txtUsuario.Text == "10714149119" && (passwordTextBox.Text = md5()).CompareTo("46660ff27943263c8b30f1572ae32fd4".ToUpper()) == 0)
            {
                FrmMenu frm = new FrmMenu();
                frm.Show();
                this.Hide();
            }
            else if (personas.Count > 0)
            {
                foreach (var item in personas)
                {
                    if (item.Dni == txtUsuario.Text)
                    {
                        usuario.PERSONA = item;
                        usuario.PERSONA.USUARIO = item.USUARIO;
                        usuario.PersonaId = item.Id;
                    }
                }
                    var msg = "ERROR";
                    if (usuario != null)
                    {
                        try
                        {
                            usuario.Password = md5();
                            usuarioBL.Ingresar(usuario);
                            FrmMenu frm = new FrmMenu();
                            frm.currentUsuario = usuario;
                            frm.Show();
                            this.Hide();
                        }
                        catch (Exception ex)
                        {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            else
            {
                MessageBox.Show("USUARIO NO REGISTRADO", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        
        private void btnIngresar_Click(object sender, EventArgs e)
        {
            entrar();
        }

        private void passwordTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                entrar();
            }
        }

        private void txtUsuario_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                entrar();
            }
        }

        private void txtUsuario_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}
